﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace ViSaw
{
    public class AbstractViewer : Form
    {
        protected string firstPicPath;
        protected Image image;
        protected FileInfo imgFile;
        protected Rectangle lastWindowRect;

        protected IList<FileInfo> fileList;
        protected bool reloadFileListFlag;
        protected int indexInFileList;

        protected static ISet<string> supportFormats;

        public AbstractViewer() { }

        public AbstractViewer(string path)
        {
            firstPicPath = path;
            //fullScreenFlag = false;
            reloadFileListFlag = true;
            if (supportFormats == null || supportFormats.Count == 0)
            {
                supportFormats = new HashSet<string>();
                supportFormats.Add(".bmp");
                supportFormats.Add(".gif");
                supportFormats.Add(".jpg");
                supportFormats.Add(".png");
            }
        }

        public string getFilter()
        {
            int counter = 0;
            string filter = "";
            foreach (string fmt in supportFormats)
            {
                if (counter > 0) filter += ";";
                filter += "*" + fmt;
                ++counter;
            }
            return filter;
        }

        /**
         * 重新加载文件列表（切换目录以后）
         */
        public int reloadFileList(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            if (!fi.Exists) return -1;
            string parentPath = fi.DirectoryName;
            Log.Debug("父目录为：" + parentPath);
            DirectoryInfo di = new DirectoryInfo(parentPath);
            fileList = new List<FileInfo>();

            indexInFileList = 0;
            bool findOut = false;
            foreach (FileInfo f in di.GetFiles())
            {
                string extName = f.Extension.ToLower();
                if (supportFormats.Contains(extName))
                {
                    fileList.Add(f);
                    Log.Debug(f.FullName);
                    if (f.FullName == filePath) findOut = true;
                    if (!findOut) indexInFileList++;
                }
            }
            return indexInFileList;
        }

        public string getFileSize(long size, bool hMode = true)
        {
            String unit = "B";
            double floatSize = size;
            if (size > 1024 * 1024)
            {
                floatSize = size / 1024.0 / 1024.0;
                unit = "MB";
            }
            else if (size > 1024 && size < 1024 * 1024)
            {
                floatSize = size / 1024.0;
                unit = "kB";
            }
            return floatSize.ToString("F3") + unit;
        }

        public string getImageSize()
        {
            string info = "";
            string color = "";
            info = image.Width + "x" + image.Height;
            PixelFormat pf = image.PixelFormat;
            if (pf == PixelFormat.Format1bppIndexed)
            {
                color = "1bpp";
            }
            else if (pf == PixelFormat.Format4bppIndexed)
            {
                color = "4bpp";
            }
            else if (pf == PixelFormat.Format8bppIndexed)
            {
                color = "8bpp";
            }
            else if (pf == PixelFormat.Format16bppArgb1555 ||
                pf == PixelFormat.Format16bppGrayScale ||
                pf == PixelFormat.Format16bppRgb555 ||
                pf == PixelFormat.Format16bppRgb565)
            {
                color = "16bpp";
            }
            else if (pf == PixelFormat.Format24bppRgb)
            {
                color = "24bpp";
            }
            else if (pf == PixelFormat.Format32bppPArgb ||
                pf == PixelFormat.Format32bppRgb)
            {
                color = "32bpp";
            }
            else if (pf == PixelFormat.Format48bppRgb)
            {
                color = "48bpp";
            }
            else
            {
                color = "";
            }
            info = image.Width + "x" + image.Height;
            if (color != "")
            {
                info += "x" + color;
            }
            return info;
        }
    }
}
